package datas;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.HashMap;

public class Graph {
	private static int current_id = 0;
	
	private int id = current_id++;
	private ArrayList<Vertex> vertices;
	private HashMap<Vertex, ArrayList<Vertex>> edges;
	
	public Graph() {
		this.vertices = new ArrayList<Vertex>();
		this.edges = new HashMap<Vertex, ArrayList<Vertex>>();
	}
	
	public Graph(int _id) {
		this.id = _id;		
		this.vertices = new ArrayList<Vertex>();
		this.edges = new HashMap<Vertex, ArrayList<Vertex>>();
	}
	
	public void renew() {
		this.vertices.clear();
		this.edges.clear();
	}
	
	public Vertex addVertex(Point2D _pos) {
		Vertex temp = new Vertex(_pos);
		
		this.vertices.add(temp);
		this.edges.put(temp, new ArrayList<Vertex>());
		
		return temp;
	}
	
	public Vertex addVertex(int _id, Point2D _pos) {
		Vertex temp = new Vertex(_id, _pos);
		
		this.vertices.add(temp);
		this.edges.put(temp, new ArrayList<Vertex>());
		
		return temp;
	}
	
	public void addEdge(Vertex _v1, Vertex _v2) {
		// DOUBLONS
		this.edges.get(_v1).add(_v2);
		this.edges.get(_v2).add(_v1);
	}
	
	public void addEdge(int _id1, int _id2) {
		Vertex v1 = null, v2 = null;
		
		for (Vertex v: this.vertices) { 
			if (v.getIndex() == _id1)
				v1 = v;
			
			if (v.getIndex() == _id2)
				v2 = v;
		}
		
		this.addEdge(v1, v2);
	}
	
	public int getId() {
		return this.id;
	}
	
	public void setId(int _id) {
		this.id = _id;
	}
	
	public Vertex getLefterVertex() {
		Vertex lefter = this.vertices.get(0);
		
		for (Vertex v: this.vertices)
			if (v.getPosition().getX() < lefter.getPosition().getX())
				lefter = v;
		
		return lefter;
	}
	
	public Vertex getRighterVertex() {
		Vertex righter = this.vertices.get(0);
		
		for (Vertex v: this.vertices)
			if (v.getPosition().getX() > righter.getPosition().getX())
				righter = v;
		
		return righter;
	}
	
	public Vertex getUpperVertex() {
		Vertex upper = this.vertices.get(0);
		
		for (Vertex v: this.vertices)
			if (v.getPosition().getY() < upper.getPosition().getY())
				upper = v;
		
		return upper;
	}
	
	public Vertex getDownerVertex() {
		Vertex downer = this.vertices.get(0);
		
		for (Vertex v: this.vertices)
			if (v.getPosition().getY() > downer.getPosition().getY())
				downer = v;
		
		return downer;
	}
	
	public void removeVertex(Vertex _v) {	
		if (this.edges.get(_v) != null) {		
			for (Vertex u: this.edges.get(_v))
				this.edges.get(u).remove(_v);
		}
		
		this.edges.remove(_v);
		this.vertices.remove(_v);
	}
	
	public int getOrder() {
		return this.vertices.size();
	}
	
	public int getSize() {
		int sum = 0;
		
		for (Vertex v: this.edges.keySet())
			sum += this.edges.get(v).size();

		return sum/2;
	}
	
	public ArrayList<Vertex> getVertices() {
		return this.vertices;
	}	
	
	public HashMap<Vertex, ArrayList<Vertex>> getEdges() {
		return this.edges;
	}
	
	public static void changeGeneralId(int id) {
		current_id = id;
	}
}
